package com.example.testapplication.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import android.view.View.OnClickListener;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import androidx.recyclerview.widget.RecyclerView;

import com.example.testapplication.R;
import com.example.testapplication.bean.Goods;

import java.util.ArrayList;

public class RecyclerLinearAdapter extends RecyclerView.Adapter<ViewHolder>  {
    private final static String TAG = "RecyclerLinearAdapter";
    private Context mContext;
    private ArrayList<Goods> mPublicArray;

    public RecyclerLinearAdapter(Context context,ArrayList<Goods> publicArray){
        mContext = context;
        mPublicArray = publicArray;
    }

    // 创建列表项的视图持有者
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 根据布局文件item_linear.xml生成视图对象
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_linear,parent,false);
        return new ItemHolder(v);
    }
    // 绑定列表项的视图持有者
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        ItemHolder holder1 = (ItemHolder) holder;
        holder1.iv_pic.setImageResource(mPublicArray.get(position).pic_id);
        holder1.tv_title.setText(mPublicArray.get(position).title);
        holder1.tv_desc.setText(mPublicArray.get(position).desc);

        holder1.ll_item.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mOnItemClickListener != null){
                    mOnItemClickListener.onItemClick(view,position);
                }
            }
        });

        holder1.ll_item.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (mOnItemLongClickListener != null) {
                    mOnItemLongClickListener.onItemLongClick(view, position);
                }
                return true;
            }
        });
    }
    // 获取列表项的个数
    @Override
    public int getItemCount() {
        return mPublicArray.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }



    // 定义列表项视图持有者
    public class ItemHolder extends RecyclerView.ViewHolder{
        public LinearLayout ll_item;
        public ImageView iv_pic;
        public TextView tv_title;
        public TextView tv_desc;
        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            ll_item = itemView.findViewById(R.id.ll_item);
            iv_pic = itemView.findViewById(R.id.iv_pic);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_desc = itemView.findViewById(R.id.tv_desc);
        }
    }

    // 定义一个循环图列表项的点击监听接口
    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }
    // 定义一个循环图列表长按监听器接口
    public interface OnItemLongClickListener{
        void onItemLongClick(View view, int position);
    }

    // 定义一个循环视图列表项的删除监听器接口
    public interface OnItemDeleteClickListener {
        void onItemDeleteClick(View view, int position);
    }

    // 声明一个列表项点击监听器对象
    private OnItemClickListener mOnItemClickListener;
    public void setOnItemClickListener(OnItemClickListener listener){
        this.mOnItemClickListener = listener;
    }

    // 声明列表项的长按监听器对象
    private OnItemLongClickListener mOnItemLongClickListener;

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        this.mOnItemLongClickListener = listener;
    }

    // 处理列表项点击事件
    public void onItemClick(View view,int position){
        String desc = String.format("您点击了第%d项，标题是%s", position + 1,
                mPublicArray.get(position).title);
        Toast.makeText(mContext, desc, Toast.LENGTH_SHORT).show();
    }

    // 处理列表项的长按事件
    public void onItemLongClick(View view, int position) {
        String desc = String.format("您长按了第%d项，标题是%s", position + 1,
                mPublicArray.get(position).title);
        Toast.makeText(mContext, desc, Toast.LENGTH_SHORT).show();
    }


}
